package com.tyt.lelan.adapter;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

import com.tyt.lelan.R;
import com.tyt.lelan.provider.FeedsData;
import com.tyt.lelan.utils.ImageUtil;
import com.tyt.lelan.utils.Property;

public class BoardAdapter extends ResourceCursorAdapter {

	/**
	 * @param context
	 * @param layout
	 * @param c
	 * @param autoRequery
	 */	
	private int[] index;
	private Cursor mCursor;
	private int mType;
	private Context mContext;
	private int page;
	
	private final int ITEM_MORE=0x1;
	
	public BoardAdapter(Context context, int layout, Cursor c,int p) {
		super(context, layout, c, true);
		// TODO Auto-generated constructor stub
		this.mCursor=c;
		page = p;
		this.mContext=context;
		int length = FeedsData.CategoryColumns.COLUMNS.length;
		index= new int[length];
		for(int i=0;i<length;i++){
			index[i] =c.getColumnIndex(FeedsData.CategoryColumns.COLUMNS[i]);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
	 */
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		  final ViewHolder cache = (ViewHolder) view.getTag();
		 
		  cache.tx_boardname.setText(cursor.getString(index[1]));
		  cache.tx_boardnumber.setText("("+cursor.getString(index[2])+")");
		 
		  
	}

	/* (non-Javadoc)
	 * @see android.widget.ResourceCursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
	 */
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = super.newView(context, cursor, parent);
		ViewHolder holder = new ViewHolder();
		holder.tx_boardname=(TextView) view.findViewById(R.id.tv_boardname);
		holder.tx_boardnumber=(TextView) view.findViewById(R.id.tv_boardnumber);
        view.setTag(holder);
		return view;
	}
	
	private class ViewHolder{
		
		private TextView tx_boardname;
		private TextView  tx_boardnumber;
	}
	
	 public int getCount() {
	      
		//判断当前页面，控制返回的数据量，实现分页
			int i = getPage() * Property.PAGE_SIZE_BOARD;
			int iEnd = i+Property.PAGE_SIZE_BOARD;
			int size=mCursor.getCount();
			if(size>i&&size<iEnd)
				return (size-i);
			else
				return Property.PAGE_SIZE_BOARD;
			
	        
	    }
	
	 
	 public View getView(int position, View convertView, ViewGroup parent) {
	       
		   super.getView(position, convertView, parent);
		    if(mCursor==null){
		    	 throw new IllegalStateException("this should only be called when the cursor is valid");
		    }
		 
		    //控制Cursor的移动
	        if (!mCursor.moveToPosition(getPage()*Property.PAGE_SIZE_BOARD+position)) {
	 
	               throw new IllegalStateException("couldn't move cursor to position " + (getPage()*Property.PAGE_SIZE_BOARD+position));
	        }
	        View v;
	        if (convertView == null) {
	            v = newView(mContext, mCursor, parent);
	        } else {
	            v = convertView;
	        }
	        bindView(v, mContext, mCursor);
	        return v;
	    }

	 
		/* (non-Javadoc)
		 * @see android.widget.CursorAdapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			Cursor mCursor = (Cursor) super.getItem(position);
			mCursor.moveToPosition(getPage()*Property.PAGE_SIZE_BOARD+position);
			return mCursor;
		}

	public void setPage(int page) {
		this.page = page;
	}


	public int getPage() {
		return page;
	}
	
}
